<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="75px"
    >
      <el-form-item label="参数名称" prop="ParaName">
        <el-input
          v-model="queryParams.ParaName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数分类" prop="snNum">
        <el-cascader
                  :options="devicesabac"
                  v-model="abcd"
                  @change="cascaderPropshhqfunasd"
                  clearable
                  placeholder="请选择设备类型"
                ></el-cascader>
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-cascader
                  :options="devicesab"
                  v-model="abc"
                  :props="cascaderProps"
                  clearable
                  @change="cascaderPropshhqfun"
                  placeholder="请选择设备类型"
                ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:manage:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="manageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" width="50" align="center" prop="paraNo1" />
      <el-table-column
        label="图标"
        align="center"
        prop="img"
      >
      <template slot-scope="scope">
          <img :src="scope.row.img" alt="" width="50px">
        </template>
    </el-table-column>
      <el-table-column
        label="参数名称"
        align="center"
        prop="paraName"
      />
      <el-table-column label="设备类型" align="center" prop="typeName" />
      <el-table-column label="参数分类"  align="center">
        <template slot-scope="scope">
          {{ scope.row.paraType == 1 ? "遥信量" : "遥测量" }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleAdd(scope.row, true)"
            v-hasPermi="['system:manage:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAdd(scope.row, false)"
            v-hasPermi="['system:manage:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:manage:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <formarea ref="formarea" ></formarea>
  </div>
</template>

<script>
import {
  listManage,
  getManage,
  delManage,
  addManage,
  updateManage,
  getCover,
  upload,
  userList,
  listCoverNum,
  listDeviceName,
} from "@/api/system/manage";
import { deflist, deleteDef,listGroup } from "@/api/system/imei";
import { pcaTextArr } from "element-china-area-data";
import MyComponent from "../../components/MyComponent.vue";
import { getToken } from "@/utils/auth";
import formarea from "./form.vue";
export default {
  components: {
    "my-component": MyComponent,
    formarea,
  },
  name: "Manage",
  dicts: ["link_type", "device_type"],
  data() {
    return {
      equipmentTypeData: [],
      //设备类型
      devices: [],
      codes1: [],
      //设备负责人
      users: [],
      userLists: [],
      fileList: [],
      dialogImageUrl: "",
      urlImg: "",
      dialogVisible: false,
      disabled: false,
      options: pcaTextArr,
      dialogTableVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        snNum: null,
        equipmentType: null,
        region: null,
        status: null,
        devTypeId: null,
      },
      // 表单参数
      form: {
        equipmentType: [],
      },
      // 表单校验
      rules: {
        code: [
          { required: true, message: "井盖编号不能为空", trigger: "blur" },
        ],
        snNum: [{ required: true, message: "设备SN号", trigger: "blur" }],
        region: [
          { required: true, message: "所属区域不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        regionId: [
          { required: true, message: "所属区域不能为空", trigger: "change" },
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" },
        ],
        latAndlng: [
          { required: true, message: "坐标位置不能为空", trigger: "blur" },
        ],
        img: [{ required: true, message: "请上传图片", trigger: "change" }],
        uid: [
          { required: true, message: "设备负责人不能为空", trigger: "blur" },
        ],
      },
      upload: {
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 图片上传的方法地址:
        url: process.env.VUE_APP_BASE_API + "/system/manage/upload",
      },
      codes: [],
      devicesab: [],
      devicesabac : [{
        label : '遥信量',
        value : 1
      },{
        label : '遥测量',
        value : 2
      }],
      cascaderProps: {
        value: "typeID",
        label: "groupName",
      },
      abc : [],
      abcd : []
    };
  },
  created() {
    this.getList();
    // this.getCode();
  },
  mounted() {
    //设备类型
    listGroup().then((res) => {
        this.devicesab = res.rows;
        console.log('hssn',this.devices);
      });
    listDeviceName(null).then((response) => {
      this.devices = response.rows;
      console.log(this.devices);
      this.total = response.total;
      this.loading = false;
    });
    userList(null).then((response) => {
      this.users = response.rows; // 将返回的用户列表数据存储在this.users中
      this.total = response.total;
    });
  },
  methods: {
    cascaderPropshhqfunasd(res){
      console.log(res);
    },
    cascaderPropshhqfun(res) {
      console.log('abc',this.abc[0]);
    },
    pointValue(value) {
      this.form.lat = value.lat;
      this.form.lng = value.lng;
      this.form.latAndlng = value.lat + "," + value.lng;
    },
    selectLatAndLng() {
      this.dialogTableVisible = true;
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      
      this.queryParams.TypeID = this.abc[0]
      this.queryParams.ParaType = this.abcd[0]
      console.log(999,this.queryParams);
      deflist(this.queryParams).then((res) => {
        console.log(312, res);
        this.manageList = res.rows;
        this.pinjie();
        this.total = res.total;
        this.loading = false;
      });
      // listManage(this.queryParams).then(response => {
      //   this.manageList = response.rows;

      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        snNum: null,
        coverId: null,
        equipmentType: null,
        regionId: null,
        region: null,
        address: null,
        lat: null,
        latAndlng: null,
        lng: null,
        latestTime: null,
        note: null,
        status: null,
        isWarn: null,
        isWarnToday: null,
        isRecoverToday: null,
        isUsed: null,
        img: null,
      };
      this.resetForm("form");
    },
    pinjie() {
      this.manageList.forEach((element) => {
        element.latAndlng = element.lat + "," + element.lng;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(data, flag) {
      this.$refs.formarea.init(data, flag);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.id;
      getManage(id).then((response) => {
        //图片回显
        this.fileList = [{ url: response.data.img }]; //加的是这一行
        this.form = response.data;
        this.form.latAndlng = response.data.lat + "," + response.data.lng;
        this.form.region = response.data.region.split("/");
        this.open = true;
        this.title = "修改设备管理";
        this.equipmentTypeData[0] = this.form.devTypeId;
        this.equipmentTypeData[1] = Number(this.form.equipmentType);
      });
    },
    //点击上传按钮
    ingUPliog() {
      upload(this.form).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.region = Array.isArray(this.form.region)
            ? this.form.region.join("/")
            : this.form.region;
          this.form.equipmentType = this.equipmentTypeData[1]
            ? this.equipmentTypeData[1]
            : "";
          this.form.devTypeId = this.equipmentTypeData[0]
            ? this.equipmentTypeData[0]
            : "";
          if (this.form.id != null) {
            console.log(11, this.form.equipmentType);
            updateManage(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let codeExists = this.codes.some(
              (item) => item.code === this.form.code
            );
            if (codeExists) {
              console.log(this.form);
              addManage(this.form)
                .then((response) => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                })
                .catch((err) => {
                  this.form.region = this.form.region.join("/");
                });
            } else {
              this.$modal.msgError("井盖编号不存在，请核对井盖编号");
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids;
      this.$confirm("是否删除该数据")
        .then((_) => {
         
          deleteDef({id:row.id}).then((res) => {
            this.$message({
                  message: "删除成功",
                  type: "success",
                });
            this.getList()
          });
          
        })
        .catch((_) => {});
    },
    handleFileChange(file, fileList) {
      this.form.fileList = file;
      console.log(this.form.fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handlebefore(file) {
      console.log(file);
    },
   
    //设备负责人下拉框数据
    getUserList() {
      console.log(12321);
      this.loading = true;
    },
    getUserfun(res) {
      console.log(res);
      this.form.uid = res;
    },
    //井盖编号
    getCodeList() {
      this.loading = true;
      listCoverNum(null).then((response) => {
        this.codes1 = response.rows; // 将返回的用户列表数据存储在this.users中
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>
